package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TUser;
import com.bwie.service.TUserService;
import com.bwie.mapper.TUserMapper;
import com.bwie.utils.R;
import com.bwie.vo.LoginInfoVo;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
* @author 28463
* @description 针对表【t_user】的数据库操作Service实现
* @createDate 2025-07-03 09:12:31
*/
@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser>
    implements TUserService{
    @Autowired
    TUserMapper tUserMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public R getlogin(LoginInfoVo loginInfoVo) {
        TUser tUser = tUserMapper.selectOne(new QueryWrapper<TUser>().lambda().eq(TUser::getUserName, loginInfoVo.getUserName()));
        if(tUser==null){
            return R.ERROR(500,"用户不存在");
        }

        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "123456").claim("userId", tUser.getUserId()).claim("userName", tUser.getUserName()).compact();
        String tokenKey="token-"+tUser.getUserId();
        stringRedisTemplate.opsForValue().set(tokenKey,token,5, TimeUnit.MINUTES);
        tUser.setToken(token);
        return R.OK(tUser);
    }
}




